import json

from apps.crawler_platform.core_platform.core_g import CoreSqlValue
from apps.crawler_platform.core_platform.g_model import DealModel, CallBackModel, \
    DealInsertModel, BaikeHomeModel, BaikeArticleModel

__all__ = [
    "baike_baikehome_callback",
    "baike_baikearticle_callback",
    "baike_wikihome_callback",
    "baike_wikiarticle_callback",
    "baike_baikehomefuzzy_callback",
]


def baike_baikehome_callback(callmodel: CallBackModel[BaikeHomeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    sign = callmodel.sql_model.home_rawid.split('_')[0]
    home_json = json.loads(callmodel.sql_model.home_json)
    class_ = home_json['fenlei']
    keyword = home_json['keyword']
    if "1_1" in data:
        d_i_model = DealInsertModel()
        d_i_model.insert_pre = CoreSqlValue.insert_ig_it
        if data["1_1"]["info_list"]["children"]:
            index = 1
            title_dict = dict()
            for info in data["1_1"]["info_list"]["children"]:
                if info['href']:
                    title = info['text'].strip()
                else:
                    title = info["text_span"].strip()
                try:
                    title_dict[title] += 1
                except:
                    title_dict[title] = 1
            for info in data["1_1"]["info_list"]["children"]:
                temp = info_dicts.copy()
                temp["task_tag"] = temp["task_tag_next"]
                del temp["task_tag_next"]
                article_json = {"keyword": keyword, "class": class_, "sign": sign, "index": index}

                if info['href']:
                    text = info['text'].strip()
                    temp["rawid"] = f"{callmodel.sql_model.home_rawid}（{text}）"
                    url = info['href']
                    if url.find("baike.baidu.com") == -1:
                        url = "https://baike.baidu.com" + url
                    url = url.split("#")[0]
                    article_json["url"] = url
                    text = info['text'].strip()
                    if title_dict[text] > 1:
                        text = text + ";;" + url.split('/')[-1]
                    temp["rawid"] = f'{callmodel.sql_model.home_rawid}（{text}）'
                else:
                    txt = info["text_span"].strip()
                    if not txt:
                        raise Exception
                    if title_dict[txt] > 1:
                        txt = txt + ";;" + data["1_1"]["url"].split('/')[-1]
                    temp["rawid"] = f'{callmodel.sql_model.home_rawid}（{txt}）'
                    article_json["url"] = data["1_1"]["url"]
                temp["article_json"] = json.dumps(article_json, ensure_ascii=False)
                d_i_model.lists.append(temp)
                index += 1
        else:
            temp = info_dicts.copy()
            temp["task_tag"] = temp["task_tag_next"]
            del temp["task_tag_next"]
            article_json = {"keyword": keyword, "class": class_, "sign": sign, "index": 1}
            title = data["1_1"]["title"].replace('_百度百科', '').strip()
            temp["rawid"] = f"{callmodel.sql_model.home_rawid}（{title}）"
            article_json["url"] = data["1_1"]["url"]
            temp["article_json"] = json.dumps(article_json, ensure_ascii=False)
            d_i_model.lists.append(temp)

        result.next_dicts.insert.append(d_i_model)
    return result


def baike_baikehomefuzzy_callback(callmodel: CallBackModel[BaikeHomeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    sign = callmodel.sql_model.home_rawid.split('_')[0]
    home_json = json.loads(callmodel.sql_model.home_json)
    class_ = home_json['fenlei']
    keyword = home_json['keyword']
    if "1_1" in data:
        d_i_model = DealInsertModel()
        d_i_model.insert_pre = CoreSqlValue.insert_ig_it
        if data["1_1"]["children"]:
            index = 1
            for info in data["1_1"]["children"]:
                temp = info_dicts.copy()
                temp["task_tag"] = temp["task_tag_next"]
                del temp["task_tag_next"]
                article_json = {"keyword": keyword, "class": class_, "sign": sign, "moth": "fuzzy", "index": index}

                if info['href']:
                    title = info['text'].replace(";", "").replace("-", "").replace("百度百科", "").strip()
                    temp["rawid"] = f"{callmodel.sql_model.home_rawid}（{title}）"
                    url = info['href']
                    if url.find("baike.baidu.com") == -1:
                        url = "https://baike.baidu.com" + url
                    article_json["url"] = url
                temp["article_json"] = json.dumps(article_json, ensure_ascii=False)
                d_i_model.lists.append(temp)
                index += 1

        result.next_dicts.insert.append(d_i_model)
    return result


def baike_baikearticle_callback(callmodel: CallBackModel[BaikeArticleModel]) -> DealModel:
    result = DealModel()
    return result


def baike_wikihome_callback(callmodel: CallBackModel[BaikeHomeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in data:
        d_i_model = DealInsertModel()
        d_i_model.insert_pre = CoreSqlValue.insert_ig_it
        if data["1_1"]['judge2']:
            for info in data["1_1"]["info_list"]["children"]:
                temp = info_dicts.copy()
                temp["task_tag"] = temp["task_tag_next"]
                del temp["task_tag_next"]
                article_json = {
                    "keyword": callmodel.sql_model.home_rawid
                }
                if info['href']:
                    temp["rawid"] = f"{callmodel.sql_model.home_rawid}（{info['text']}）"
                    article_json["url"] = f"https://zh.wikipedia.org{info['href']}"
                else:
                    temp["rawid"] = data["1_1"]["title"].split(" - ")[0].strip()
                    article_json["url"] = data["1_1"]["url"]
                temp["article_json"] = json.dumps(article_json, ensure_ascii=False)
                d_i_model.lists.append(temp)
        else:
            temp = info_dicts.copy()
            temp["task_tag"] = temp["task_tag_next"]
            del temp["task_tag_next"]
            article_json = {
                "keyword": callmodel.sql_model.home_rawid
            }
            temp["rawid"] = data["1_1"]["title"].split(" - ")[0].strip()
            article_json["url"] = data["1_1"]["url"]
            temp["article_json"] = json.dumps(article_json, ensure_ascii=False)
            d_i_model.lists.append(temp)
        result.next_dicts.insert.append(d_i_model)
    return result


def baike_wikiarticle_callback(callmodel: CallBackModel[BaikeArticleModel]) -> DealModel:
    result = DealModel()
    return result
